package com.beyondsoft.mcms.domain;

import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.validation.constraints.NotNull;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findAdvicesByConferenceAndMemberInfo", "findAdvicesByConference" })
public class Advice {

	/**
     */
	@NotNull
	@ManyToOne
	private Conference conference;

	/**
     */
	@NotNull
	@ManyToOne
	private MemberInfo memberInfo;

	/**
     */
	@NotNull
	private String createdBy;

	/**
     */
	private String updatedBy;

	/**
     */
	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(style = "MM")
	private Date createDate;

	/**
     */
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(style = "MM")
	private Date updateDate;

	public static TypedQuery<Advice> findAdvicesByConferenceAndMemberInfo(Conference conference, MemberInfo memberInfo) {
		EntityManager em = Advice.entityManager();
		if (conference == null && memberInfo == null) {
			TypedQuery<Advice> q = em.createQuery("SELECT o FROM Advice AS o ", Advice.class);
			return q;
		} else if (conference != null && memberInfo == null) {
			TypedQuery<Advice> q = em.createQuery("SELECT o FROM Advice AS o WHERE o.conference = :conference ",
					Advice.class);
			q.setParameter("conference", conference);
			return q;
		} else if (conference == null && memberInfo != null) {
			TypedQuery<Advice> q = em.createQuery("SELECT o FROM Advice AS o WHERE o.memberInfo = :memberInfo",
					Advice.class);
			q.setParameter("memberInfo", memberInfo);
			return q;
		} else {
			TypedQuery<Advice> q = em.createQuery(
					"SELECT o FROM Advice AS o WHERE o.memberInfo = :memberInfo AND o.conference = :conference ",
					Advice.class);
			q.setParameter("memberInfo", memberInfo);
			q.setParameter("conference", conference);
			return q;
		}
	}

	public static TypedQuery<Conference> findConferencesByMemberInfoAndConferenceStatus(MemberInfo memberInfo,
			Item conferenceStatus) {
		EntityManager em = Advice.entityManager();
		if (memberInfo == null && conferenceStatus == null) {
			TypedQuery<Conference> q = em.createQuery(
					"SELECT o.conference FROM Advice AS o where o.conference.conferenceStatus.id !=401",
					Conference.class);
			return q;
		} else if (conferenceStatus != null && memberInfo == null) {
			TypedQuery<Conference> q = em
					.createQuery(
							"SELECT o.conference FROM Advice AS o WHERE o.conference.conferenceStatus = :conferenceStatus and o.conference.conferenceStatus.id !=401",
							Conference.class);
			q.setParameter("conferenceStatus", conferenceStatus);
			return q;
		} else if (conferenceStatus == null && memberInfo != null) {
			TypedQuery<Conference> q = em
					.createQuery(
							"SELECT o.conference FROM Advice AS o WHERE o.memberInfo = :memberInfo and o.conference.conferenceStatus.id !=401",
							Conference.class);
			q.setParameter("memberInfo", memberInfo);
			return q;
		} else {
			TypedQuery<Conference> q = em
					.createQuery(
							"SELECT o.conference FROM Advice AS o WHERE o.memberInfo = :memberInfo AND o.conference.conferenceStatus = :conferenceStatus and o.conference.conferenceStatus.id !=401 ",
							Conference.class);
			q.setParameter("memberInfo", memberInfo);
			q.setParameter("conferenceStatus", conferenceStatus);
			return q;
		}
	}

	public static TypedQuery<Advice> findAdvicesByMemberInfo(MemberInfo memberInfo, Long conferenceStatus) {
		if (memberInfo == null)
			throw new IllegalArgumentException("The memberInfo argument is required");
		EntityManager em = Advice.entityManager();
		TypedQuery<Advice> q = em
				.createQuery(
						"SELECT o FROM Advice AS o WHERE o.memberInfo = :memberInfo and o.conference.conferenceStatus.id = :conferenceStatus",
						Advice.class);
		q.setParameter("memberInfo", memberInfo);
		q.setParameter("conferenceStatus", conferenceStatus);
		return q;
	}
}
